package com.kingscup.gamestates;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import com.kingscup.Storage;

public class PlayerNameMenu extends BasicGameState {
	
	private int stateID;
	
	AngelCodeFont font = PlayerMenu.font;
	String[] playerNames = PlayerMenu.playerNames;
	
	Image playerSheet;
	
	TextField enterName1;
	TextField enterName2;
	TextField enterName3;
	TextField enterName4;
	TextField enterName5;
	TextField enterName6;

	public PlayerNameMenu(int stateID) {
		this.stateID = stateID;
	}

	@Override
	public void init(GameContainer gameContainer, StateBasedGame sbg)
			throws SlickException {
		playerSheet = new Image("assets/playersheet.png");
		enterName1 = new TextField(gameContainer, font, 30, 570, 110, 30,
				new ComponentListener() {
					public void componentActivated(AbstractComponent source) {
						playerNames[0] = enterName1.getText();
						enterName2.setFocus(true);
					}
				});
		enterName2 = new TextField(gameContainer, font, 140, 570, 110, 30, new ComponentListener() {
			public void componentActivated(AbstractComponent source) {
				playerNames[1] = enterName2.getText();
				
				try {
					enterName3.setFocus(true);
				} catch(NullPointerException e) {
					enterName1.setFocus(true);
				};
			}
		});
		enterName3 = new TextField(gameContainer, font, 250, 570, 110, 30, new ComponentListener() {
			public void componentActivated(AbstractComponent source) {
				playerNames[2] = enterName3.getText();

				try {
					enterName4.setFocus(true);
				} catch(NullPointerException e) {
					enterName1.setFocus(true);
				};
			}
		});
		enterName4 = new TextField(gameContainer, font, 360, 570, 110, 30, new ComponentListener() {
			public void componentActivated(AbstractComponent source) {
				playerNames[3] = enterName4.getText();

				try {
					enterName5.setFocus(true);
				} catch(NullPointerException e) {
					enterName1.setFocus(true);
				};
			}
		});
		enterName5 = new TextField(gameContainer, font, 470, 570, 110, 30, new ComponentListener() {
			public void componentActivated(AbstractComponent source) {
				playerNames[4] = enterName5.getText();

				try {
					enterName6.setFocus(true);
				} catch(NullPointerException e) {
					enterName1.setFocus(true);
				};
			}
		});
		enterName6 = new TextField(gameContainer, font, 580, 570, 110, 30, new ComponentListener() {
			public void componentActivated(AbstractComponent source) {
				playerNames[5] = enterName6.getText();
				enterName1.setFocus(true);
			}
		});
	}

	@Override
	public void render(GameContainer gameContainer, StateBasedGame sbg,
			Graphics g) throws SlickException {
		if (Storage.players == 2) {
			playerSheet.getSubImage(0, 0, 150, 150).draw(10, 450);
			playerSheet.getSubImage(150, 0, 150, 150).draw(120, 450);
		}
		if (Storage.players == 3) {
			playerSheet.getSubImage(0, 0, 150, 150).draw(10, 450);
			playerSheet.getSubImage(150, 0, 150, 150).draw(120, 450);
			playerSheet.getSubImage(300, 0, 150, 150).draw(230, 450);
		}
		if (Storage.players == 4) {
			playerSheet.getSubImage(0, 0, 150, 150).draw(10, 450);
			playerSheet.getSubImage(150, 0, 150, 150).draw(120, 450);
			playerSheet.getSubImage(300, 0, 150, 150).draw(230, 450);
			playerSheet.getSubImage(0, 150, 150, 150).draw(340, 450);
		}
		if (Storage.players == 5) {
			playerSheet.getSubImage(0, 0, 150, 150).draw(10, 450);
			playerSheet.getSubImage(150, 0, 150, 150).draw(120, 450);
			playerSheet.getSubImage(300, 0, 150, 150).draw(230, 450);
			playerSheet.getSubImage(0, 150, 150, 150).draw(340, 450);
			playerSheet.getSubImage(150, 150, 150, 150).draw(450, 450);
		}
		if (Storage.players == 6) {
			playerSheet.getSubImage(0, 0, 150, 150).draw(10, 450);
			playerSheet.getSubImage(150, 0, 150, 150).draw(120, 450);
			playerSheet.getSubImage(300, 0, 150, 150).draw(230, 450);
			playerSheet.getSubImage(0, 150, 150, 150).draw(340, 450);
			playerSheet.getSubImage(150, 150, 150, 150).draw(450, 450);
			playerSheet.getSubImage(300, 150, 150, 150).draw(560, 450);
		}


	}

	@Override
	public void update(GameContainer gameContainer, StateBasedGame sbg, int delt)
			throws SlickException {
		// TODO Auto-generated method stub

	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return stateID;
	}

}
